package cn.kmia.hrm.service.basicData;

import cn.kmia.hrm.pojo.PostNatureInfo;
import cn.kmia.hrm.service.BaseService;
import cn.kmia.hrm.util.CommonSet;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: hrm
 * @description: 岗位属性信息服务
 * @author: lmx
 * @create: 2018/8/21 16:42
 **/
@IocBean
public class PostNatureInfoService extends BaseService {

    /**
    * @Description: 新增
    * @param  [postNatureInfo, userID]
    * @return cn.kmia.hrm.pojo.PostNatureInfo
    * @throws
    * @author lmx
    * @date 2018/8/23 13:46
    */
    public PostNatureInfo add(PostNatureInfo postNatureInfo,String userID){
        postNatureInfo.setOpBy(userID);
        postNatureInfo.setCreateTime(new Timestamp(new Date().getTime()));
        postNatureInfo.setUpdateTime(new Timestamp(new Date().getTime()));
        postNatureInfo = dao.insert(postNatureInfo);
        updatePostNatureInfo();
        return postNatureInfo;
    }

    /**
    * @Description: 更新postNatureInfo，更新成功：返true，更新不成功：返false
    * @param  [postNatureInfo, userID]
    * @return boolean
    * @throws
    * @author lmx
    * @date 2018/8/23 14:20
    */
    public boolean update(PostNatureInfo postNatureInfo,String userID){
        postNatureInfo.setOpBy(userID);
        postNatureInfo.setUpdateTime(new Timestamp(new Date().getTime()));
        if(dao.updateIgnoreNull(postNatureInfo) > 0){
            updatePostNatureInfo();
            return true;
        }else
            return false;
    }

    /**
    * @Description: 删除对应id数组的PostNatureInfo对象
    * @param  [ids]
    * @return org.nutz.lang.util.NutMap
    * @throws
    * @author lmx
    * @date 2018/8/23 14:45
    */
    public NutMap del(int[] ids){
        NutMap nm = new NutMap();
        if(ids != null && ids.length > 0){
            dao.update(PostNatureInfo.class,Chain.make("delFlag",true),Cnd.where("id","in",ids));
            updatePostNatureInfo();
            nm.setv("success",true);
        }else {
            nm.setv("success",false).setv("errorMsg","请重试!");
        }
        return  nm;
    }

    /**
    * @Description: 分页查询
    * @param  [pager, natureName]
    * @return org.nutz.dao.QueryResult
    * @throws
    * @author lmx
    * @date 2018/8/23 15:25
    */
    public QueryResult queryByPage(Pager pager,String natureName){
        Cnd cnd = Cnd.where("1","=",1);
        if(!Strings.isBlank(natureName) && !"null".equals(natureName)){
            cnd.and("post_nature_name","like","%"+natureName+"%");
        }
        cnd.and("delFlag","is",null);
        List<PostNatureInfo> list = dao.query(PostNatureInfo.class,cnd,pager);
        QueryResult qr = new QueryResult();
        qr.setList(list);
        pager.setRecordCount(dao.count(PostNatureInfo.class,cnd));
        qr.setPager(pager);
        return qr;
    }

    public List<PostNatureInfo> query(String natureName){
        return dao.query(PostNatureInfo.class,Cnd.where("delFlag","is",null).and("post_nature_name","like","%"+natureName+"%"));
    }


    /**
    * @Description: 新增或更新属性后重新获取PostNatureInfo的list
    * @param  []
    * @return void
    * @throws
    * @author lmx
    * @date 2018/8/23 13:40
    */
    public void updatePostNatureInfo(){
        CommonSet.PostNatureInfo = new ArrayList<>();
        List<String> natureName = this.queryPNatrueName();
        for(String nn:natureName){
            CommonSet.PostNatureInfo.add(nn);
        }
    }

    /**
    * @Description: 查询属性名并放入list
    * @param  []
    * @return java.util.List<java.lang.String>
    * @throws
    * @author lmx
    * @date 2018/8/23 13:41
    */
    public List<String> queryPNatrueName(){
        Sql sql = Sqls.create("select post_nature_name from post_nature_info $condition");
        sql.setCondition(Cnd.where("delFlag","is",null));
        sql.setCallback(new SqlCallback() {
            public Object invoke(Connection connection, ResultSet resultSet, Sql sql) throws SQLException {
                List<String> natureName = new ArrayList<>();
                while (resultSet.next())
                    natureName.add(resultSet.getString("post_nature_name"));
                return natureName;
            }
        });
        dao.execute(sql);
        return sql.getList(String.class);
    }
}
